/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkGb28181/MkGb28181.h"

typedef struct _catalog_item
{
    MkString Address;
    MkString CivilCode;
    MkString DeviceID;
    MkString Manufacturer;
    MkString Model;
    MkString Name;
    MkString Owner;
    MkString ParentID;
    MkString Parental;
    MkString RegisterWay;
    MkString SafetyWay;
    MkString Secrecy;
    MkString Status;
}MkGbCatalogItem;

typedef struct _cruise_point
{
    MkString    CruisePreindex;
    MkString    CruiseStayTime;
    MkString    CruiseSpeed;
}MkGbCruisePoint;

typedef struct _cruise_track
{
    MkString                            CruiseId;
    MkString                            CruiseName;
    MkMap<MkString,MkGbCruisePoint>     MapCruiseTrack;  //key preset index 
}MkCruiseTrack;

//class CMkRtpPool;
class CMkRtpUdpServer;
class CMkRtpTcpServer;
class CMkSampleGbDevice : public CMkGb28181
{
    typedef struct _play_param
    {
        BOOL                bRealplay;
        MkString            PlaySession;
        CMkRtpUdpServer*    PlayUdpServer;
        CMkRtpTcpServer*    PlayTcpServer;
    }MkSampleGbPlayParam;
public:
    CMkSampleGbDevice(CMkEventPool* pEventPool, CMkTimer* pTimer);
    ~CMkSampleGbDevice();

    Uint32 StartDevice(const MkGbStartParam& Param);
    Uint32 SetServerInfo(const MkString& GbId, const MkString& GbRealm, const MkString& GbHost, const Uint16& GbPort, const BOOL& bTcp);
    Uint32 SetLocalInfo(const MkString& UserName, const MkString& Password, const Uint32& Expries, const Uint32 HeartBeatInterval, const Uint32& HeartBeatCount);
protected:
private:
    Uint32 RegisterToGbServer(const CMkGbDeviceAuthorRequest& Req);

    Uint32 OnGbRequest(const CMkGbRequest* pGbReq, const MkGbResponseCallback& Cb);
    Uint32 OnGbMessageRequest(const CMkGbMessageRequest* pGbReq, const MkGbResponseCallback& Cb);
    Uint32 OnGbSubscribeRequest(const CMkGbSubscribeRequest* pGbReq, const MkGbResponseCallback& Cb);
    Uint32 OnNotifyRequest(const CMkGbNotifyRequest* pGbReq, const MkGbResponseCallback& Cb);
    Uint32 OnGbInviteRequest(const CMkGbInviteReqeust* pGbReq, const MkGbResponseCallback& Cb);

    Uint32 PacketDeviceStatus(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketCatalog(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketDeviceInfo(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketRecordInfo(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketConfigDownload(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketPresetQuery(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketHomePositionQuery(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketCruiseTrackListQuery(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketCruiseTrackQuery(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketPTZPosition(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketSDCardStatus(const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketDeviceConfig(const MkString& DeviceId, const MkGbMessage& ReqMessage, MkGbMessage& GbMessage);
    Uint32 PacketDeviceControl(const MkString& DeviceId, const MkGbMessage& ReqMessage, CMkGbMessageResponse** ppRes, CMkGbMessageResponse& Res);
    Uint32 HasControlType(const MkGbMessage& ReqMessage, const MkString& ControlType,MkString& StrValue);

    Uint32 NotifyKeepAlive();
    Uint32 NotifyAlarm();  //5.3
    Uint32 NotifyUpgradeSuccess(); //5.9
    Uint32 NotifyMobilePosition(); //5.6
    Uint32 NotifyUploadSnapShotFinished();  //5.7  //5.5 broadcast device not has
    Uint32 NotifyCatalog();
    Uint32 NotifyPtzPosition();
    Uint32 VideoUploadNotify();//TODO what?  5.8
    Uint32 MediaStatusNotify();  //5.4

    Uint32 StartGetAudio(const MkString& SourceId);
private:
    //CMkRtpPool* m_pRtpPool;

    MkString    m_ServerId;
    MkString    m_ServerRealm;
    MkString    m_ServerHost;
    Uint16      m_ServerPort;
    BOOL        m_bServerTcp;

    MkString    m_DeviceUserName;
    MkString    m_DevicePassword;
    Uint32      m_DeviceExpries;
    Uint32      m_HeartBeatInterval;
    Uint32      m_HeartBeatCount;

    MkString    m_DeviceName;
    MkMap<Uint8, MkString>   m_VecPreset;
    MkGbCatalogItem m_ChannelItem;
    MkGbCatalogItem m_AlarmItem;
    MkGbCatalogItem m_AudioOutItem;
    BOOL            m_bAlarmSetGuard;
    BOOL            m_bAlarming;
    MkString        m_TestAlarmTimer;       //sample use a timer to send alarm
    BOOL            m_bRecording;
    MkMapString     m_HomePositonParam;

    MkGbMessage m_SVACEncodeConfig;
    MkGbMessage m_BasicParam;
    MkGbMessage m_SVACDecodeConfig;
    MkGbMessage m_VideoParamOpt;
    MkGbMessage m_VideoParamAttribute;
    MkGbMessage m_VideoRecordPlan;
    MkGbMessage m_VideoAlarmRecord;
    MkGbMessage m_PictureMask;
    MkGbMessage m_AlarmReport;
    MkGbMessage m_OSDConfig;
    MkGbMessage m_SnapShot;
    Uint8       m_FrameMirrorType;
    MkMapString m_MapPreset;//key preset id value preset name
    MkMap<MkString, MkCruiseTrack>   m_MapCruiseTrack;

    MkString    m_RegisterTimer;
    MkString    m_KeepAliveTimer;
    BOOL        m_bOnline;
    CMkTime     m_LastKeepAliveTime;

    BOOL        m_bSubscribeCatalog;
    MkString    m_SubscribeCatalogId;
    Uint32      m_SubscribeCatalogExpires;
    MkString    m_SubscribeCatalogEvent;
    MkString    m_CatalogTimer;

    BOOL        m_bSubscribePtzPosition;
    MkString    m_SubscribePtzPositionId;
    Uint32      m_SubscribePtzPositionExpires;
    MkString    m_SubscribePtzPositionEvent;
    MkString    m_PtzPositionTimer;

    Uint32          m_MobilePostionInterval;
    BOOL            m_bSubscribeMobilePosition;
    MkString        m_SubscribeMobilePositionEvent;
    Uint32          m_SubscribeMobilePositionExpires;
    MkString        m_SubscribeMobilePositionId;
    MkString        m_MobilePostionTimer;
    MkString        m_UpdateSessionId;
    
    MkMap<MkString, MkSampleGbPlayParam> m_MapPlayParam;
};